﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IPMS.Models
{
    public class PackageModel
    {
       
        private DataContextDataContext dataContext;
        public PackageModel()
        {
            this.dataContext = new DataContextDataContext();
        }
        public List<Package> GetListPackage(int projectID)
        {
            return this.dataContext.GetListPackage(projectID).ToList();
        }
        public int CreatePackage(Package package)
        {
            try
            {
                int identity = Convert.ToInt32(this.dataContext.CreatePackage(package.Name, package.ProjectID, package.DecisionNumber, package.DecisionDate, package.LCP, package.FCP, package.CurrentRate, package.Time, package.BiddingTypeID, package.ApproveLevel, package.SuperVisiorID).FirstOrDefault().Column1.Value);
                return identity;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public List<BiddingType> GetAllBiddingTypes()
        {
            return this.dataContext.GetAllBiddingTypes().ToList();
        }
        public Package GetPackageByID(int id)
        {
            return this.dataContext.GetPackageByID(id).FirstOrDefault();
        }
        public bool CheckUserPackagePermission(int userID, int packageID)
        {
            return this.dataContext.CheckPackageApprovePermission(userID, packageID).Value;
        }
        public bool ApprovePackage(int id, int userID)
        {
            try
            {
                this.dataContext.ApprovePackage(id, userID);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public List<BiddingInviting> GetListInviting(int packageID)
        {
            return this.dataContext.GetListInviting(packageID).ToList();
        }
        public int CreateInviting(BiddingInviting inviting, params string[] bidderIDs)
        {
            try
            {
                int identity = (int)this.dataContext.CreateInviting(inviting.PackageID, inviting.Number, inviting.DecisionDate, inviting.PresentedDate, inviting.InvitingDate, inviting.ApproveLevel, inviting.DocumentURL, inviting.Note).FirstOrDefault().Column1.Value;
                if (bidderIDs != null)
                {
                    foreach (string id in bidderIDs)
                    {
                        this.dataContext.InsertBidderInvited(Convert.ToInt32(id), identity);
                    }
                }
                return identity;
            }
            catch
            {
                return 0;
            }
        }
    }
}